﻿using System.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Services;
using YAKEJI.Abp.Database;
using YAPACS.BusinessObjects.Exams;
using YAPACS.BusinessObjects.Managements.Exams;
using YAPACS.RisServer.Abstractions;

namespace YAPACS.RisServer.Services
{
    public class CommonUse(IConfiguration configuration, IDatabaseConnectionProvider _connectionProvider) : DomainService, ICommonUse, ITransientDependency
    {
        private readonly IConfiguration _configuration = configuration;
        private readonly IDatabaseConnectionProvider connectionProvider = _connectionProvider;

        /// <summary>
        /// DateTime转化秒级时间戳
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public long DateTimeToUnixTimestampSeconds(DateTime dateTime)
        {
            DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            TimeSpan timeSpan = dateTime.ToUniversalTime() - epoch;
            return (long)timeSpan.TotalSeconds;
        }

        public static long ToUnixTimestampSeconds(DateTime dateTime)
        {
            DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            TimeSpan timeSpan = dateTime.ToUniversalTime() - epoch;
            return (long)timeSpan.TotalSeconds;
        }

        /// <summary>
        /// PatientSex类型枚举值转化为ApplicableSex类型枚举值
        /// </summary>
        /// <param name="sex"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApplicableSex PatientSexToApplicableSex(PatientSex sex)
        {
            ApplicableSex result = new ApplicableSex();
            switch (sex)
            {
                case PatientSex.Male:
                    result = ApplicableSex.Male;
                    break;
                case PatientSex.Female:
                    result = ApplicableSex.Female;
                    break;
                case PatientSex.Other:
                    result = ApplicableSex.None;
                    break;
                default:
                    break;
            }
            return result;
        }

        /// <summary>
        /// 获取周一的日期
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public DateTime MondayDateOfISOWeek(DateTime dateTime)
        {
            DateTime monday = dateTime;
            DayOfWeek dayOfWeek = dateTime.DayOfWeek;
            int dayDiff = (int)dayOfWeek - (int)DayOfWeek.Monday;
            if (dayDiff < 0) // 周日
            {
                dayDiff += 6; // -1 +7 =6
            }

            monday = dateTime.AddDays(-dayDiff).Date;
            return monday;
        }

        /// <summary>
        /// 获取Dapper 连接对象
        /// </summary>
        /// <returns></returns>
        public IDbConnection GetDapperConnection(out DatabaseType databaseType)
        {
            string connectionStr = _configuration.GetConnectionString("Default").ToString();
            int index = connectionStr.IndexOf("MySql");
            if (index >= 0)
            {
                databaseType = DatabaseType.MySql;
                connectionStr = connectionStr.Replace("XpoProvider=MySql;", "");
            }
            else
            {
                databaseType = DatabaseType.SqlServer;
                connectionStr = connectionStr.Replace("XpoProvider=SqlServer;", "");
            }
            return connectionProvider.GetDbConnection(connectionStr, databaseType);
        }
    }
}
